package com.klikmee.sms.core.base;

import java.util.List;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.dao.BulkMessageDAO;
import com.klikmee.sms.core.base.dao.ProviderFacadeLocal;
import com.klikmee.sms.core.base.util.Constants;
import com.klikmee.sms.core.base.util.States;
import com.klikmee.sms.core.model.BulkMessage;
import com.klikmee.sms.core.model.Provider;
import com.klikmee.sms.core.model.ServiceChargingCategory;

public class SimpleBulkMessageCreatorImpl implements BulkMessageCreator {
	private Core				core;
	private ProviderFacadeLocal	providerDAO;
	private BulkMessageDAO		bulkMessageDAO;
	private Provider			mtsProvider;
	private Provider			telenorProvider;
	private Provider			vipProvider;
	private Logger				logger	= Logger.getLogger( SimpleBulkMessageCreatorImpl.class );

	public SimpleBulkMessageCreatorImpl() {

	}

	public void setCore( Core core ) {
		this.core = core;
	}

	public void setProviderDAO( ProviderFacadeLocal providerDAO ) {
		this.providerDAO = providerDAO;
	}

	public void setBulkMessageDAO( BulkMessageDAO bulkMessageDAO ) {
		this.bulkMessageDAO = bulkMessageDAO;
	}

	public void init() {
		this.vipProvider = providerDAO.findByName( Constants.PROVIDER_VIP );
		this.telenorProvider = providerDAO.findByName( Constants.PROVIDER_TELENOR );
		this.mtsProvider = providerDAO.findByName( Constants.PROVIDER_MTS );
	}

	public int createBulkMessage( String text, List<String> receiverIds, String signatureText, ServiceChargingCategory serviceChargingCategory ) {

		int bulkCount = 0;

		for (String currentReceiverId : receiverIds) {

			// verify phone number
			Provider provider = resolveProvider( currentReceiverId );
			if (provider == null) {
				// invalid receiver id, skip it
				logger.warn( "Invalid phone number detected during sending bulk message [" + currentReceiverId + "]" );
			}

			// continue with sending bulk message
			else {
				BulkMessage bulkMessage = new BulkMessage();
				bulkMessage.setText( text );
				bulkMessage.setSenderId( signatureText );
				bulkMessage.setToNumber( currentReceiverId );
				bulkMessage.setProvider( provider );
				bulkMessage.setChargingCategory( serviceChargingCategory );
				// TODO setting sender id in bulk messages

				bulkMessage.setState( States.BulkMessage.WAITING_TO_BE_SENT );

				// store bulk message into database
				bulkMessageDAO.create( bulkMessage );
				// submit message to bulk queue
				core.enqueueBulkMessage( bulkMessage );
				// increment count
				bulkCount++;

			}
		}
		// after processing all receiver IDs, return bulk count
		return bulkCount;
	}

	public Provider resolveProvider( String receiverId ) {
		if (receiverId.matches( mtsProvider.getMobilePhonePrefixes() )) {
			return mtsProvider;
		} else if (receiverId.matches( vipProvider.getMobilePhonePrefixes() )) {
			return vipProvider;
		} else if (receiverId.matches( telenorProvider.getMobilePhonePrefixes() )) {
			return telenorProvider;
		} else {
			return null;
		}
	}

	public void setMtsProvider( Provider mtsProvider ) {
		this.mtsProvider = mtsProvider;
	}

	public void setTelenorProvider( Provider telenorProvider ) {
		this.telenorProvider = telenorProvider;
	}

	public void setVipProvider( Provider vipProvider ) {
		this.vipProvider = vipProvider;
	}
}
